package me.firstapp.service.shop.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.module.bbs.Member;
import me.firstapp.module.shop.Order;
import me.firstapp.module.shop.OrderItem;
import me.firstapp.module.shop.Product;
import me.firstapp.module.shop.ProductComment;
import me.firstapp.repository.bbs.MemberRepository;
import me.firstapp.repository.shop.OrderItemRepository;
import me.firstapp.repository.shop.OrderRepository;
import me.firstapp.repository.shop.ProductCommentRepository;
import me.firstapp.repository.shop.ProductRepository;
import me.firstapp.service.shop.ProductCommentService;

@Service
public class ProductCommentServiceImpl implements ProductCommentService {

	@Autowired
	private ProductCommentRepository productCommentRepository;

	@Autowired
	private ProductRepository productRepository;

	@Autowired
	private MemberRepository memberRepository;

	@Autowired
	private OrderRepository orderRepository;

	@Autowired
	private OrderItemRepository orderItemRepository;

	@Override
	public List<ProductComment> findProductCommentList(Long productId, Long apiClientId) {
		if (productId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "商品id");
		}
		return productCommentRepository.findProductCommentList(productId, apiClientId);
	}

	@Override
	public ProductComment addProductComment(String orderNo, Long productId, String content, Long fromMemberId,
			Long apiClientId) {
		if (StrUtils.isNULL(orderNo)) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "订单号");
		}
		if (productId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "商品id");
		}
		if (StrUtils.isNULL(content)) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "评论内容");
		}
		if (fromMemberId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "评论人id");
		}
		Order order = orderRepository.findOrderByOrderNo(orderNo, apiClientId);
		if (order == null) {
			throw new ServiceException(StatusHouse.ORDER_NOT_EXIST);
		}
		Product product = (Product) productRepository.findProductById(productId, apiClientId);
		if (product == null) {
			throw new ServiceException(StatusHouse.PRODUCT_NOT_EXIST);
		}
		Member member = (Member) memberRepository.findMemberById(fromMemberId, apiClientId);
		if (member == null) {
			throw new ServiceException(StatusHouse.MEMBER_NOT_FOUND);
		}
		// BuyRecord buyRecord =
		// buyRecordRepository.findByMemberIdAndOrderNoAndProductId(fromMemberId,
		// orderNo,
		// productId);
		// if (buyRecord == null) {
		// throw new ServiceException(StatusHouse.CAN_NOT_COMMENT_PRODUCT);
		// }
		ProductComment comment = productCommentRepository.findByOrderNoAndMemberIdAndProductId(orderNo, fromMemberId,
				productId, apiClientId);
		if (comment != null) {
			throw new ServiceException(StatusHouse.COMMENT_PRODUCT_ONECE_IN_ORDER);
		}

		comment = new ProductComment();
		comment.setContent(content);
		comment.setCreateTime(new Date());
		comment.setFromMember(member);
		comment.setProductId(productId);
		comment.setOrderNo(orderNo);
		comment.setApiClientId(apiClientId);
		productCommentRepository.save(comment);

		List<OrderItem> items = orderItemRepository.findOrderItemsByOrderId(order.getId());
		for (OrderItem orderItem : items) {
			if (productId == orderItem.getProduct().getId()) {
				orderItem.setCommentStatus(1);
				orderItemRepository.update(orderItem);
			}
		}

		return comment;
	}

}
